import { addInvoiceApi } from '../../../api/order'
module.exports = Behavior({
  behaviors: [],
  properties: {
    info: {
      type: Object,
      value: {}
    }
  },
  data: {
    validateMap: {
      Personal: ['title'],
      Enterprise: ['title', 'unitTaxNum'],
      AddedValue: [
        'title',
        'unitTaxNum',
        'registerAddress',
        'registerPhone',
        'accountBank',
        'bankNum'
      ]
    }
  },
  attached: function () {},
  methods: {
    onChange(e) {
      const key = e.currentTarget.dataset.key
      const changeKey = `info.${key}`
      this.setData({
        [changeKey]: e.detail
      })
    },
    validate() {
      let validate = true
      let validateList = []
      const { invoiceType, titleType } = this.data.info
      if (Number(invoiceType) == 1) {
        if (Number(titleType) == 1) {
          validateList = this.data.validateMap.Personal
        } else {
          validateList = this.data.validateMap.Enterprise
        }
      } else {
        validateList = this.data.validateMap.AddedValue
      }
      for (let i = 0; i < validateList.length; i++) {
        const key = validateList[i]
        if ((this.data.info[key] ?? '') === '') {
          validate = false
          break
        }
      }
      return validate
    },
    async subimit() {
      if (!this.validate()) {
        wx.showToast({
          title: '请完善信息',
          icon: 'none'
        })
        return ['err', null]
      }
      const [err] = await addInvoiceApi(this.data.info)
      if (err) return [err, null]
      return [undefined, 'success']
    }
  }
})
